package com.liusany.myshop.web.admin;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.ui.Model;

import com.liusany.modules.mapper.JsonMapper;

public class BaseCopntroller {
	public static  String redirectUrl = null;
	public static final String SUCCESS = "/admin/success";
	public static final String ERROR = "/admin/error/";
	public static final String STATUS = "status";
	public static final String MESSAGE = "message";
	
	public static JsonMapper mapper = JsonMapper.buildNonDefaultMapper();
	
	
	
	// 输出JSON成功消息，返回null
	public Map<String, String> ajaxJsonSuccessMessage(String message) {
		Map<String, String> jsonMap = new HashMap<String, String>();
		jsonMap.put(STATUS, "success");
		jsonMap.put(MESSAGE, message);
		return jsonMap;
	}
	
	// 输出JSON成功消息，返回null
	public Map<String, String> ajaxJsonErrorMessage(String message) {
		Map<String, String> jsonMap = new HashMap<String, String>();
		jsonMap.put(STATUS, "erroe");
		jsonMap.put(MESSAGE, message);
		return jsonMap;
	}
	
		
	// AJAX输出，返回null
	public String ajax(String content, String type,HttpServletResponse response) {
		try {
			
			response.setContentType(type + ";charset=UTF-8");
			response.setHeader("Pragma", "No-cache");
			response.setHeader("Cache-Control", "no-cache");
			response.setDateHeader("Expires", 0);
			response.getWriter().write(content);
			response.getWriter().flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String getRedirectUrl() {
		return redirectUrl;
	}

	public static void setRedirectUrl(String redirectUrl) {
		BaseCopntroller.redirectUrl = redirectUrl;
	}
	
	/**
	 * 操作成功后的处理
	 * @param redirectUrl 页面重定向页面
	 * @param message 操作处理消息
	 * @param model 保存信息model对象
	 */
	public void setRedirectInfo(String redirectUrl,String message,Model model){
		model.addAttribute("message", message);
		model.addAttribute("redirectUrl", redirectUrl);
	}
		
}
